package com.sophimp.jumphack;

import java.awt.image.BufferedImage;

/**
 * 目标物查找
 */
public class BaseTargetFinder {

    protected static int R_TARGET = 93;
    protected static int G_TARGET = 68;
    protected static int B_TARGET = 62;

    /**
     * 扫描的 y 区域
     */
    public int RANGE_MAX_Y = Constant.MAX_Y;
    public int RANGE_MIN_Y = Constant.MIN_Y;

    /**
     * 更新当前Y扫描区域
     */
    public void updateYRange(int minY, int maxY){
        RANGE_MIN_Y = minY;
        RANGE_MAX_Y = maxY;
    }

    public int[] findLeft(BufferedImage image) {
        if (image == null) {
            return null;
        }
        int width = image.getWidth();
        int height = image.getHeight();

        int[] ret = {0, 0};
        int maxX = Integer.MIN_VALUE;
        int minX = Integer.MAX_VALUE;
        int maxY = -1;
        int minY = -1;
        // 找脚后跟, 高度 20px
        // 从右向左找
        for (int i = Constant.MIDDLE_X; i >= Constant.MIN_X; i--) {
            for (int j = RANGE_MAX_Y; j >= RANGE_MIN_Y; j--) {
                int pixel = image.getRGB(i, j);
                int r = (pixel & 0xff0000) >> 16;
                int g = (pixel & 0xff00) >> 8;
                int b = (pixel & 0xff);
                if (ToleranceHelper.match(r, g, b, R_TARGET, G_TARGET, B_TARGET, 16)) {
                    maxX = Integer.max(maxX, i);
                    minX = Integer.min(minX, i);
                    if (maxY == -1){
                        maxY = j;
                        minY = maxY - 10;
                    }
                }
                if (minY > 0 && j <= minY){
                    break;
                }
            }
        }

        ret[0] = (maxX + minX) / 2 +20;
        ret[1] = minY;
        System.out.println(maxX + ", " + minX);
        System.out.println("pos, x: " + ret[0] + ", y: " + ret[1]);
        return ret;
    }

    public int[] findRight(BufferedImage image) {
        if (image == null) {
            return null;
        }
        int width = image.getWidth();
        int height = image.getHeight();

        int[] ret = {0, 0};
        int maxX = Integer.MIN_VALUE;
        int minX = Integer.MAX_VALUE;
        int maxY = -1;
        int minY = -1;
        // 找脚后跟, 高度 20px
        // 从右向左找
        for (int i = Constant.MIDDLE_X; i < Constant.MAX_X; i++) {
            for (int j = RANGE_MAX_Y; j >= RANGE_MIN_Y; j--) {
                int pixel = image.getRGB(i, j);
                int r = (pixel & 0xff0000) >> 16;
                int g = (pixel & 0xff00) >> 8;
                int b = (pixel & 0xff);
                if (ToleranceHelper.match(r, g, b, R_TARGET, G_TARGET, B_TARGET, 16)) {
//                if (ToleranceHelper.match(r, g, b, R_TARGET_RIGHT, G_TARGET_RIGHT, B_TARGET_RIGHT, 16)) {
                    maxX = Integer.max(maxX, i);
                    minX = Integer.min(minX, i);
                    if (maxY == -1){
                        maxY = j;
                        minY = maxY - 10;
                    }
                }
                if (minY > 0 && j <= minY){
                    break;
                }
            }
        }

        ret[0] = (maxX + minX) / 2 +15;
        ret[1] = minY;
        System.out.println(maxX + ", " + minX);
        System.out.println("pos, x: " + ret[0] + ", y: " + ret[1]);
        return ret;
    }

    public static int[] buildArray(int i, int j) {
        int[] ret = {i, j};
        return ret;
    }

    public boolean invalid(int x, int y) {
        if (x < Constant.MIN_X || x > Constant.MAX_X || y < Constant.MIN_Y || y > Constant.MAX_Y) {
            // 非法坐标， 退出来
            System.out.println(getClass().getSimpleName() + " find nextCenter, invalid, (" + x + ", " + y + ")");
            return false;
        }
        return true;
    }
}
